package com.codeshellme.bookmanager.controller;

import com.codeshellme.bookmanager.dto.BookBorrow;
import com.codeshellme.bookmanager.service.BookBorrowService;
import com.codeshellme.bookmanager.utils.CommonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BookBorrowController {

    @Autowired
    private BookBorrowService bookBorrowService;

    @RequestMapping("/book_borrow")
    public String book_borrow() {
        return "book_borrow";
    }

    /**
     * 分页查询借阅信息
     * @param book_name     书名
     * @param type_id       图书类型
     * @param author_id     作者
     * @param status        借阅状态
     * @param student_id    学生学号
     * @param student_name  学生姓名
     * @param bdate_start   借书起始时间
     * @param bdate_end     借书终止时间
     * @param rdate_start   还书起始时间
     * @param rdate_end     还书终止时间
     * @param page          page
     * @param rows          rows
     */
    @ResponseBody
    @RequestMapping("/queryAllBookBorrows")
    public CommonResult queryAllBookBorrows(String book_name, Integer type_id, Integer author_id, String status,
                                            Integer student_id, String student_name,
                                            String bdate_start, String bdate_end,
                                            String rdate_start, String rdate_end,
                                            Integer page, Integer rows) {

        PageInfo<Map<String, Object>> pageInfo = bookBorrowService.queryAllBookBorrows(
                book_name, type_id, author_id, status,
                student_id, student_name,
                bdate_start, bdate_end,
                rdate_start, rdate_end,
                page, rows);

        // 构建 EasyUI 分页需要的数据
        Map<String, Object> ret = new HashMap<>();
        ret.put("rows", pageInfo.getList());
        ret.put("total", pageInfo.getTotal());

        return CommonResult.success(ret);
    }


    // 还书
    @ResponseBody
    @RequestMapping("/bookReturn")
    public CommonResult bookReturn(@RequestParam(value = "id") Integer borrowId,
                                   @RequestParam(value = "stock_id") Integer stockId) {
        int ret = bookBorrowService.bookReturn(borrowId, stockId);
        return CommonResult.success("还书成功！");
    }

    // 借书保存
    @ResponseBody
    @RequestMapping(value = "/bookBorrowSave")
    public CommonResult bookBorrowSave(@RequestBody List<BookBorrow> borrow_infos) {

        try {
            bookBorrowService.bookBorrowSave(borrow_infos);
            return CommonResult.success("借书成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("借书失败！可能是库存不足");
        }
    }

}
